package h0924;

import java.util.Arrays;

/**
  * @description 轮转数组
  * @author 不知名帅哥
  * @date 2024/9/24 10:04
  * @version 1.0
*/
public class RotateArray {
    public static void main(String[] args) {
int[] nums={1,2,3,4,5,6};
int k=2;
rotate(nums,k);
        System.out.println(Arrays.toString(nums));
    }
    public static void rotate(int[] nums, int k) {
       //使用三次反转
        int n= nums.length;
        k=k%n;
        reverse(nums,0,n-1);
        reverse(nums,0,k-1);
        reverse(nums,k,n-1);
    }

    private static void reverse(int[] nums, int start, int end) {
        while (start<end){
            int temp=nums[start];
            nums[start]=nums[end];
            nums[end]=temp;
            start++;
            end--;
        }
    }
}
